﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Sinacor.Infra.Tools;
using System.Collections.Generic;

namespace RightsRegistrationWeb
{
    public partial class Grid : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.LoadDataGrid();
        }

        private void LoadDataGrid()
        {
            //DataAccess dataAccess = new DataAccess();
            //grdRights.DataSource = dataAccess.LoadRights();
            //grdRights.DataBind();
        }

        //    //List<Module> modules;

        //    foreach (Right r in dataAccess.LoadRights())
        //    {
        //        //modules = dataAccess.LoadModules(r.CodigoDireito);

        //        //string module = string.Empty;

        //        //foreach (Module item in modules)
        //        //{
        //        //    module = module + item.Sigla + ",";
        //        //}

        //        //if (!string.IsNullOrEmpty(module))
        //        //    module = module.Substring(0, module.Length - 1);

        //        HtmlTableRow row = new HtmlTableRow();
        //        row.Attributes.Add("onmouseover", "GridOnMouseOver(this)");
        //        row.Attributes.Add("onmouseout", "GridOnMouseOut(this)");
        //        row.Attributes.Add("onDblclick", string.Format("GridMouseDblClick('datagridRights', this, '{0}')", r.SiglaModulos));
        //        row.Attributes.Add("onclick", "GridMouseClick('datagridRights', '" + r.CodigoDireito.ToString() + "', true)");
        //        row.Attributes.Add("codigotipodireito", r.CodigoTipoDireito.ToString());
        //        row.Attributes.Add("codigotipooperacao", r.CodigoTipoOperacao.ToString());
        //        row.Attributes.Add("style", "cursor: hand");
        //        row.BgColor = "#ffffff";

        //        HtmlTableCell cellCheckBox = new HtmlTableCell();
        //        HtmlTableCell cellCodigo = new HtmlTableCell();
        //        HtmlTableCell cellDescricao = new HtmlTableCell();
        //        HtmlTableCell cellTipoDireito = new HtmlTableCell();
        //        HtmlTableCell cellTipoOperacao = new HtmlTableCell();

        //        cellCheckBox.Align = "center";
        //        cellCodigo.Align = "center";

        //        cellCheckBox.Controls.Add(new RightsCheckbox(r.CodigoDireito.ToString(), false));
        //        cellCodigo.InnerHtml = r.CodigoDireito.ToString();
        //        cellDescricao.InnerHtml = r.DescricaoDireito;
        //        cellTipoDireito.InnerHtml = r.DescricaoTipoDireito;

        //        switch (r.CodigoTipoOperacao)
        //        {
        //            case "C":
        //                cellTipoOperacao.InnerHtml = "Create";
        //                break;
        //            case "R":
        //                cellTipoOperacao.InnerHtml = "Read";
        //                break;
        //            case "U":
        //                cellTipoOperacao.InnerHtml = "Update";
        //                break;
        //            case "D":
        //                cellTipoOperacao.InnerHtml = "Delete";
        //                break;
        //            case "E":
        //                cellTipoOperacao.InnerHtml = "Execute";
        //                break;
        //        }                

        //        row.Cells.Add(cellCheckBox);
        //        row.Cells.Add(cellCodigo);
        //        row.Cells.Add(cellDescricao);
        //        row.Cells.Add(cellTipoDireito);
        //        row.Cells.Add(cellTipoOperacao);

        //        dataGrid.Rows.Add(row);
        //    }
        //}

        protected void grdRights_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdRights.PageIndex = e.NewPageIndex;
            LoadDataGrid();            
        }

        protected void grdRights_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Right r = (e.Row.DataItem as Right);
                Label lbl = (Label)e.Row.FindControl("lblGrdTipoOperacao");
                if (lbl != null)
                {
                    switch (r.CodigoTipoOperacao)
                    {
                        case "C":
                            lbl.Text = "Create";
                            break;
                        case "R":
                            lbl.Text = "Read";
                            break;
                        case "U":
                            lbl.Text = "Update";
                            break;
                        case "D":
                            lbl.Text = "Delete";
                            break;
                        case "E":
                            lbl.Text = "Execute";
                            break;
                    }
                }

                e.Row.Attributes.Add("onmouseover", "GridOnMouseOver(this)");
                e.Row.Attributes.Add("onmouseout", "GridOnMouseOut(this)");
                e.Row.Attributes.Add("onDblclick", string.Format("GridMouseDblClick('datagridRights', this, '{0}')", r.SiglaModulos));
                e.Row.Attributes.Add("onclick", "GridMouseClick('datagridRights', '" + r.CodigoDireito.ToString() + "', true)");
                e.Row.Attributes.Add("codigotipodireito", r.CodigoTipoDireito.ToString());
                e.Row.Attributes.Add("codigotipooperacao", r.CodigoTipoOperacao.ToString());
                e.Row.Attributes.Add("style", "cursor: hand");
            }
        }
    }
}
